package com.vortex.cloud.sample.utils;

import com.github.liaochong.myexcel.core.DefaultExcelBuilder;
import com.github.liaochong.myexcel.core.strategy.WidthStrategy;
import com.github.liaochong.myexcel.utils.AttachmentExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-20 周三
 * @time 16:07
 * @description
 */
public class MyExcelUtils {

    /**
     * @Description: 默认导出
     * @Param: [list]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T> void exportDefault(List<T> list, Class<T> clazz, String fileName, HttpServletResponse response) throws Exception {
        Assert.notEmpty(list, "数据不能为空");
        Assert.hasLength(fileName, "文件名不能为空");
        Workbook workbook = DefaultExcelBuilder.of(clazz).build(list);
        AttachmentExportUtil.export(workbook, fileName, (HttpServletResponse) response);
    }

    /**
     * @Description: 默认计算宽度等导出
     * @Param: [list, fileName, response]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T> void exportWithAutoWidth(List<T> list, Class<T> clazz, String fileName, HttpServletResponse response) throws Exception {
        Assert.notEmpty(list, "数据不能为空");
        Assert.hasLength(fileName, "文件名不能为空");
        Workbook workbook = DefaultExcelBuilder.of(clazz).widthStrategy(WidthStrategy.AUTO_WIDTH).build(list);
        AttachmentExportUtil.export(workbook, fileName, response);
    }

    /**
     * @Description: 加密导出
     * @Param: [response]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T> void exportEncrypt(List<T> list, Class<T> clazz, String fileName, String password, HttpServletResponse response) throws Exception {
        Assert.notEmpty(list, "数据不能为空");
        Assert.hasLength(fileName, "文件名不能为空");
        Assert.hasLength(fileName, "密码不能为空");
        Workbook workbook = DefaultExcelBuilder.of(clazz).build(list);
        AttachmentExportUtil.encryptExport(workbook, fileName, response, password);
    }

    /**
     * @Description: 无样式导出
     * @Param: [list, fileName, response]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T> void exportWithNoStyle(List<T> list, Class<T> clazz, String fileName, HttpServletResponse response) throws Exception {
        Assert.notEmpty(list, "数据不能为空");
        Assert.hasLength(fileName, "文件名不能为空");
        Workbook workbook = DefaultExcelBuilder.of(clazz).noStyle().build(list);
        AttachmentExportUtil.export(workbook, fileName, response);
    }

    /**
     * @Description: 自定义样式导出
     * @Param: [response]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T> void exportCustomStyle(List<T> list, Class<T> clazz, String fileName, HttpServletResponse response) throws Exception {
        Assert.notEmpty(list, "数据不能为空");
        Assert.hasLength(fileName, "文件名不能为空");
        Workbook workbook = DefaultExcelBuilder.of(clazz)
                .style("title->color:red", "cell->color:black", "font-size:40px", "text-align:center", "font-style:italic",
                        "border-style:thin", "height: 20px", "width: 30px", "text-decoration: underline", "even->color:pink")
                .build(list);
        AttachmentExportUtil.export(workbook, fileName, response);
    }

    /**
     * @Description: 多表导出 --  TODO 多表封装有问题
     * @Param: [list, fileNames, response]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T> void exportMultiTable(List<List<T>> list, List<String> fileNames, Class<T> clazz, HttpServletResponse response) throws Exception {
        Assert.notEmpty(list, "数据不能为空");
        Assert.notEmpty(fileNames, "文件名不能为空");
        Assert.isTrue(list.size() == fileNames.size(), "数据和表名不匹配");
        Workbook workbook = null;
        for (int i = 0; i < list.size(); i++) {
            workbook = DefaultExcelBuilder.of(clazz, workbook).sheetName(fileNames.get(i + 1)).widthStrategy(WidthStrategy.NO_AUTO).build(list.get(i));
        }
        AttachmentExportUtil.export(workbook, fileNames.get(0), response);
    }

    /**
     * @Description: 动态导出-指定标题、字段顺序
     * @Param: [dataList, titles, clazz, fileName, order, response]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T> void exportDynamic(List<T> dataList, List<String> titleList, Class<T> clazz, String fileName, List<String> orderList,
                                         String password, HttpServletResponse response) throws Exception {
        Assert.notEmpty(dataList, "数据不能为空");
        Assert.hasText(fileName, "文件名不能为空");
        Workbook workbook = DefaultExcelBuilder.of(clazz)
                .sheetName(fileName)
                .titles(titleList)
                .fieldDisplayOrder(orderList)
                .build(dataList);
        AttachmentExportUtil.encryptExport(workbook, fileName, response, password);
    }

    /**
     * @Description: 动态导出--字段分组
     * @Param: [dataList, clazz, groupClazz, fileName, response]
     * @return: void
     * @Author: 王林
     * @Date: 2021/10/20
     */
    public static <T, E> void exportGroup(List<T> dataList, Class<T> clazz, Class<E> groupClazz, String fileName, HttpServletResponse response) throws Exception {
        Assert.notEmpty(dataList, "数据不能为空");
        Assert.hasText(fileName, "文件名不能为空");
        Workbook workbook = DefaultExcelBuilder.of(clazz)
                .groups(groupClazz)
                .build(dataList);
        AttachmentExportUtil.export(workbook, fileName, response);
    }

}
